import java.io.File
import java.io.FileReader
import java.io.FileWriter

/**
 * @author JiJunpeng create on 2018/4/25
 */

//val folderPath = "/Users/jijunpeng/Work/MSXF/AbsCloud/absCloud/absCloud_web/src/main"
val folderPath = "/Users/jijunpeng/Work/MSXF/AbsCloud/absCloud/absCloud_service/src/main"
//val filePath = "source1.txt"
val filePath = "source2.txt"
val tmpFileList = emptyList<String>().toMutableList()
val targetFile = File(filePath).also {
    if (!it.exists()) {
        it.createNewFile()
    }
}

val fileWriter = FileWriter(targetFile)

fun main(args: Array<String>) {
    tmpFileList.clear()
    val fileList = listFile(folderPath)
    println("***********************")
    println("***********************")
    println("***********************")
    println("***********************")
    println("***********************")
    println("***********************")
    fileList.forEach {
        println("正在复制： $it")
        var inComment = false
        FileReader(it).apply {
            this.readLines().forEach write@{
                if (it.isNotBlank() && it.isNotEmpty() && !it.trim().startsWith("//")) {
                    var newLine = it
                    if (!inComment && newLine.contains("/**")) {
                        val index = newLine.indexOf("/**")
                        newLine = newLine.substring(0, index) + "\n" + newLine.substring(index)
                        inComment = true
                        val newText = newLine.substring(0, newLine.indexOf("/**")).trim()
                        if (newText.isNotEmpty() && it.isNotBlank() && !it.trim().startsWith("//")) {
                            fileWriter.write(newText)
                            fileWriter.write("\n")
                        }
                        return@write
                    }
                    if (inComment && newLine.contains("*/")) {
                        val index = newLine.indexOf("*/")
                        newLine = newLine.substring(0, index) + "\n" + newLine.substring(index)
                        inComment = false
                        val newText = newLine.substring(newLine.lastIndexOf("*/") + 2).trim()
                        if (newText.isNotEmpty() && it.isNotBlank() && !it.trim().startsWith("//")) {
                            fileWriter.write(newText)
                            fileWriter.write("\n")
                        }
                        return@write
                    }
                    if (inComment) {
                        return@write
                    }
                    if (newLine.contains("\n")) {
                        val firstIndex = newLine.indexOf("\n")
                        val lastIndex = newLine.indexOf("\n")
                        if (firstIndex == lastIndex) {
                            if (it.isNotBlank() && it.isNotEmpty() && !it.trim().startsWith("//")) {
                                fileWriter.write(newLine.substring(0, firstIndex))
                                fileWriter.write("\n")
                            }
                        } else {
                            if (it.isNotBlank() && it.isNotEmpty() && !it.trim().startsWith("//")) {
                                fileWriter.write(newLine.substring(0, firstIndex) + newLine.substring(lastIndex))
                                fileWriter.write("\n")
                            }
                        }
                    } else {
                        fileWriter.write(newLine)
                        fileWriter.write("\n")
                    }
                }
            }
            fileWriter.flush()
        }.close()
    }
    fileWriter.close()
}

fun listFile(folderPath: String): List<String> {
    val folder = File(folderPath)
    folder.listFiles().sorted().forEach {
        if (!it.isHidden) {
            val filePath = it.absolutePath
            println("******  $filePath")
            if (it.isFile) {
                tmpFileList.add(filePath)
            } else if (it.isDirectory) {
                listFile(it.absolutePath)
            }
        }
    }
    return tmpFileList
}